package com.huyn.baseframework.dynamicload;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ryg.dynamicload.internal.DLPluginPackage;

/**
 * Created by huyaonan on 16/5/5.
 * 当在HOST中引用插件的Fragment时,插件的fragment必须继承DLFragment
 */
public class DLFragment extends Fragment {

    private DLPluginPackage mPluginPackage;
    protected DLBridge mBridge;

    public DLFragment() {}

    public void injectPlugin(DLPluginPackage mPlugin) {
        this.mPluginPackage = mPlugin;
    }

    public Resources getDLResources() {
        if(mPluginPackage == null)
            return getResources();
        return mPluginPackage.resources;
    }

    public AssetManager getAssertManager() {
        if(mPluginPackage == null)
            return getAssertManager();
        return mPluginPackage.assetManager;
    }

    public View inflate(LayoutInflater inflater, int id, ViewGroup root, boolean attachToRoot) {

        final XmlResourceParser parser = getDLResources().getLayout(id);
        try {
            return inflater.inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

    public DLPluginPackage getDLPluginPackage() {
        return mPluginPackage;
    }

    public void setDLBridge(DLBridge mBridge) {
        this.mBridge = mBridge;
    }

}
